<?php namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;

/**
 * 命题企业
 * Class PropositionCompany
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property string $code 代码
 * @property string $works_types 作品类别
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\WorksType[] $worksTypes
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Award[] $awards
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereCode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereWorksTypes($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\PropositionCompany whereDeletedAt($value)
 * @mixin \Eloquent
 */
class PropositionCompany extends Model
{
    use SoftDeletes;
    use ValidatingTrait;

    protected $table = 'proposition_companies';

    protected $dates = ['deleted_at'];

    protected $fillable = ['name', 'code', 'works_types'];

    protected $rules = [
        'name'        => 'required',
        'code'        => 'required',
       // 'works_types' => 'required',
    ];

    protected $validationMessages = [
        'name.required'        => '名称必填',
        'code.required'        => '代码必填',
       // 'works_types.required' => '上传扩展名必填',
    ];

    /**
     * 用于select的列表
     * @return array
     */
    public static function getListData()
    {
        $infos = self::all();
        $listData = ['0' => '请选择'];
        foreach ($infos as $info) {
            $listData[$info->id] = $info->name;
        }
        return $listData;
    }

    /**
     * 对应的作品类别
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function worksTypes()
    {
        return $this->belongsToMany('\App\Models\WorksType', 'company_works_type', 'company_id', 'works_type_id')
            ->withPivot( 'award_id');
    }

    /**
     * 对应的奖项
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function awards()
    {
        return $this->belongsToMany('\App\Models\Award', 'award_company', 'company_id', 'award_id');
    }
}